Slip 15


Q.1. Create an ANN and train it on house price dataset classify the house price is above 
average or below average.

import numpy as np
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.neural_network import MLPClassifier
from sklearn.metrics import accuracy_score, classification_report

# ======================================
# 1. Create synthetic house dataset
# ======================================
np.random.seed(42)
n_samples = 1000

# Features
rooms = np.random.randint(1, 8, n_samples)
area = np.random.randint(500, 4000, n_samples)   # in sqft
age = np.random.randint(0, 50, n_samples)        # age of house
location_score = np.random.randint(1, 10, n_samples)  # 1-10 rating

# Price formula (synthetic)
price = (rooms * 50000) + (area * 200) - (age * 1000) + (location_score * 10000) \
        + np.random.randint(-20000, 20000, n_samples)

# Combine features
X = pd.DataFrame({
    'rooms': rooms,
    'area': area,
    'age': age,
    'location_score': location_score
})
y = price

# ======================================
# 2. Binary target: above/below average price
# ======================================
threshold = np.mean(y)
y_class = (y > threshold).astype(int)  # 1 = above average, 0 = below average

# ======================================
# 3. Train-test split
# ======================================
X_train, X_test, y_train, y_test = train_test_split(X, y_class, test_size=0.2, random_state=42)

# ======================================
# 4. Scale features
# ======================================
scaler = StandardScaler()
X_train = scaler.fit_transform(X_train)
X_test = scaler.transform(X_test)

# ======================================
# 5. Build ANN
# ======================================
model = MLPClassifier(hidden_layer_sizes=(16, 8), activation='relu',
                      solver='adam', max_iter=500, random_state=42)

# ======================================
# 6. Train the model
# ======================================
model.fit(X_train, y_train)

# ======================================
# 7. Evaluate model
# ======================================
y_pred = model.predict(X_test)
print("Accuracy:", accuracy_score(y_test, y_pred))
print("\nClassification Report:\n", classification_report(y_test, y_pred))

Q.2. Write a python program to implement Multiple Linear Regression for a house price 
dataset.   

# Import necessary libraries
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
from sklearn.metrics import mean_squared_error, r2_score
import matplotlib.pyplot as plt
import seaborn as sns

# Step 1: Load the dataset
# You can replace 'house_prices.csv' with your actual dataset file
df = pd.read_csv("house_prices.csv")

print("✅ Dataset loaded successfully!\n")
print("First 5 rows:\n", df.head(), "\n")

# Step 2: Check for missing values
print("Null values in dataset:\n", df.isnull().sum(), "\n")

# (Optional) Remove rows with null values
df = df.dropna()

# Step 3: Define features (X) and target (y)
# Assuming dataset has columns like: 'Area', 'Bedrooms', 'Bathrooms', 'Price'
# Modify column names as per your dataset
X = df[['Area', 'Bedrooms', 'Bathrooms']]   # independent variables
y = df['Price']                             # dependent variable

# Step 4: Split the dataset into training and testing sets (80%-20%)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

# Step 5: Create and train the Multiple Linear Regression model
model = LinearRegression()
model.fit(X_train, y_train)

# Step 6: Make predictions
y_pred = model.predict(X_test)

# Step 7: Evaluate the model
mse = mean_squared_error(y_test, y_pred)
r2 = r2_score(y_test, y_pred)

print("📊 Model Evaluation:")
print("Mean Squared Error:", round(mse, 2))
print("R² Score:", round(r2, 4))
print("\nIntercept (b0):", round(model.intercept_, 2))
print("Coefficients (b1, b2, b3...):")
for col, coef in zip(X.columns, model.coef_):
    print(f"  {col}: {round(coef, 2)}")

# Step 8: Compare actual vs predicted prices
comparison = pd.DataFrame({'Actual Price': y_test, 'Predicted Price': y_pred})
print("\nComparison of Actual vs Predicted Prices:\n", comparison.head())

# Step 9: Visualize Actual vs Predicted values
plt.figure(figsize=(6,4))
sns.scatterplot(x=y_test, y=y_pred, color='blue')
plt.xlabel("Actual Prices")
plt.ylabel("Predicted Prices")
plt.title("Actual vs Predicted House Prices")
plt.show()

# Step 10: Example prediction for new input
# Example: Predict price for a house with given features
new_data = pd.DataFrame({'Area': [2500], 'Bedrooms': [4], 'Bathrooms': [3]})
predicted_price = model.predict(new_data)
print(f"\n🏠 Predicted price for new house = ${predicted_price[0]:.2f}")